#!/usr/bin/env python
from lib.format_time import *
from lib.HandleMysql import *
from lib.switch_code import *
from shop_conn import *
import sys
import re
reload(sys)
sys.setdefaultencoding('utf-8')

def get_channel_data(date):
    
    print "get_user_action_data begin : %s"%date
    
    stat_conn = set_stats_conn()
    shop_conn = set_shop_conn()
    prod_conn = set_product_conn()
    conn = set_order_conn()
    stat_table = "channel_source_stats"
    
    channel_name='zizhu'
    query = "select count(1),count(case when refer=108 then 1 end), count(case when refer=102 then 1 end) from t_order where refer<>4 and date(createdate)='%s'"%date
    items = conn.get_results(query)
    total_count = items[0][0]
    su8_count = items[0][1]
    xiaozhu_count = items[0][2]
    zizhu_count = total_count - su8_count-xiaozhu_count
    
    items=[[108,su8_count],[102,xiaozhu_count],[0,zizhu_count]]
    for item in items:
        refer = item[0]
        order_count =item[1]
        
        refer_cond = " in (%s)"%refer
        if refer == 108:
            channel_name='su8'
        elif refer == 102:
            channel_name='xiaozhu'
        else:
            channel_name='zizhu'
            refer_cond = " not in (4,106,108) "
        
        query_tmp="select count(distinct a.orderid),sum(a.totalfee),count(distinct a.buyerid),sum(b.num) from t_order a left join t_orderproduct b on a.orderid=b.orderid where a.refer %s and a.state>3 and date(a.createdate)='%s'"%(refer_cond,date)
        items_tmp = conn.get_results(query_tmp)
        pay_order_count = items_tmp[0][0]
        pay_amount = items_tmp[0][1]
        pay_user_count = items_tmp[0][2]
        sell_product_count=items_tmp[0][3]
        
        if sell_product_count==None:
            sell_product_count = 0
        if pay_amount== None:
            pay_amount =0.0
            
        pay_user_price = 0
        if pay_user_count>0:
            pay_user_price=pay_amount/pay_user_count
            
        query_tmp="select count(distinct a.orderid),sum(a.ordertotal),count(distinct a.buyerid),sum(b.num) from t_order a left join t_orderproduct b on a.orderid=b.orderid where a.refer %s and date(a.refunddate)='%s'"%(refer_cond,date)
        items_tmp = conn.get_results(query_tmp)
        refund_order_count = items_tmp[0][0]
        refund_amount = items_tmp[0][1]
        refund_user_count = items_tmp[0][2]
        refund_product_count=items_tmp[0][3]

        query_tmp="select count(distinct orderid),sum(num) from t_orderbook where refer %s and operationtime='%s'"%(refer_cond,date)
        items_tmp = conn.get_results(query_tmp)
        book_order_count = items_tmp[0][0]
        book_product_count = items_tmp[0][1]
        
        param=[date,refer,channel_name,0,0,0,0,0,0,order_count,book_product_count,book_order_count,0,sell_product_count,pay_order_count,pay_user_count,pay_user_price,pay_amount,0,refund_order_count,book_product_count,refund_amount,refund_user_count,0,0]
        stat_conn.insert_record(stat_table,param)

#logic
def execute(date):
    # get main data
    # from order orderproduct orderticket orderpay orderlog
    get_channel_data(date)
    pass

def main():

   #date = '2012-05-28'
   #execute(date)   
   if len(sys.argv)==1:
        start_date=get_yesterday()
        end_date=start_date
   elif len(sys.argv)==2:
        start_date=format_date(sys.argv[1])
        end_date=start_date
   elif len(sys.argv)==3:
        start_date=format_date(sys.argv[1])
        end_date=format_date(sys.argv[2])
   stat_date=start_date
   while stat_date>=start_date and stat_date<=end_date:
        print "The program of the date of %s is beginning at %s " %(stat_date,get_now())
        #
        execute( stat_date) 
        print "The program of the date of %s is end at %s " %(stat_date,get_now())
        stat_date=var_date(stat_date)


if __name__=='__main__':
    main()
